//
//  JaneNetWorkRequestManager.m
//  EPA
//
//  Created by lanou3g on 16/4/15.
//  Copyright © 2016年 张静. All rights reserved.
//

#import "JaneNetWorkRequestManager.h"

@interface JaneNetWorkRequestManager ()
//@property (nonatomic,copy) ResultBlock block;
@end
@implementation JaneNetWorkRequestManager
//单例方法
+ (instancetype)defaultManager {
    static JaneNetWorkRequestManager *manager = nil;
    @synchronized (self) {
        if (!manager) {
            manager = [[JaneNetWorkRequestManager alloc]init];
        }
    }
    return manager;
}
//请求方法
+ (void)requestURL:(NSURL *)url RequestMethod:(URLRequestMethod )method Params:(NSDictionary *)param Result:(ResultBlock)resultBlock {
//    空的可变字符串
    NSMutableString *urlString = [NSMutableString new];
    if ([url isKindOfClass:[NSURL class]]) {
        //拼接url
        [urlString appendString:url.absoluteString];
    }else if ([url isKindOfClass:[NSString class]]) {
        [urlString appendString:(NSString *)url];
    }if (urlString.length == 0) {
        NSError *error = [NSError errorWithDomain:@"EPA" code:251 userInfo:@{@"reason":@"你输入的网址不正确"}];
        resultBlock(nil,nil,error);
        return;
    }
//    把参数字典转成字符串
    JaneNetWorkRequestManager *manager = [JaneNetWorkRequestManager new];
    //manager.block = resultBlock;
    NSString *paraStr = [manager strFormDict:param];
    switch (method) {
        case CK_GET:
        {
            if (paraStr.length > 0) {
                [urlString appendFormat:@"?%@",paraStr];
            }
            [urlString appendString:paraStr];
            [manager requestUrl:[NSURL URLWithString:urlString] resultBlock:resultBlock];
        }
            break;
            case CK_POST:
            [manager postUrl:urlString paraStr:paraStr ResultBlock:resultBlock];
            break;
            
        default:
            break;
    }
}
//    get网络请求
- (void)requestUrl:(NSURL *)url resultBlock :(ResultBlock)block {
    //__weak typeof(self) weakSelf = self;
    NSURLSessionDataTask *dataTask = [[NSURLSession sharedSession]dataTaskWithURL:url completionHandler:block];
//    开始网络请求
    [dataTask resume];
}

//    post网络请求
- (void)postUrl:(NSString *)urlStr paraStr:(NSString *)paraStr ResultBlock:(ResultBlock)block {
//    创建URL
    NSURL *url = [NSURL URLWithString:urlStr];
//   创建可变请求
    NSMutableURLRequest *mRequest = [NSMutableURLRequest requestWithURL:url];
//    设置post
    [mRequest setHTTPMethod:@"POST"];
//    请求体
    [mRequest setHTTPBody:[paraStr dataUsingEncoding:NSUTF8StringEncoding]];
    
    NSURLSessionDataTask *dataTask = [[NSURLSession sharedSession]dataTaskWithRequest:mRequest completionHandler:block];
    [dataTask resume];
}

- (NSString *)strFormDict:(NSDictionary *)dict {
//    空数组
    NSMutableArray *array = [NSMutableArray new];
    for (NSString *key in dict) {
        NSString *str = [NSString stringWithFormat:@"%@=%@",key,dict[key]];
        [array addObject:str];
    }
//    数组拼接字符串
    NSString *para = [array componentsJoinedByString:@"&"];
    NSLog(@"%@",para);
//    返回
    return para;
}
@end
